let vm = new Vue({
  el: "#app",
  data: {
    isDicesLoading: false, //摇骰子请求中
    dices: [4, 4, 4, 4, 1, 1], //[4,4,4,4,1,1]
    voiceArr: [{
      src: './images/voice/audio_mny.mp3',
      title: "闽南语"
    }, {
      src: './images/voice/audio_pth.mp3',
      title: "普通话"
    }],
    isShowVoiceLanguage: false,
    voiceIndex: 1,
    isShowTaskPopup: false,
    isShowCoinPopup: false,
    isShowRedirectConfirm: false,
    redirectRadioVal: true,
    isShowYszResult:true
  },
  mounted() {
    //公告滚动
    this._initSwipe();
    this._voiceInstance = this._initVoice();
  },
  methods: {
    redirectConfirm(){
      alert("xxxxxxxxxxxxxx")
    },
    chooseVoice(index) {
      this._voiceInstance = this._initVoice();
      this.voiceIndex = index;
      this.isShowVoiceLanguage = false;
    },
    //开始摇骰子
    startDices() {
      this.isDicesLoading = true;
      this.dices = [];
      this._voiceInstance.play("tt");
      //请求接口以后
      setTimeout(() => {
        this.isDicesLoading = false;
        this.dices = [4, 4, 4, 4, 1, 1];
        this._voiceInstance.play(6);
      }, 1500)
    },
    _initSwipe() {
      new Swiper('.notice-list', {
        autoplay: 2500,
        direction: 'vertical'
      });

      new Swiper('.prize-list', {
        pagination: ".swiper-pagination"
      });
    },
    //初始化音频
    _initVoice() {
      let spriteData = {};
      if (this.voiceIndex == 1) {
        //普通话
        spriteData = {
          tt: { //摇的声音
            start: 36.5,
            length: 1
          },
          0: { //没中
            start: 0,
            length: 2.5
          },
          1: {//一秀
            start: 2.5,
            length: 2.8
          },
          2: { //二举
            start: 5.3,
            length: 2.7
          },
          3: { //四进
            start: 8.0,
            length: 2.5
            
          },
          4: { //三红
            start: 10.5,
            length: 3
          },

          5: { //对堂
            start: 13.5,
            length: 2.5
          },
          6: { //状元
            start: 16,
            length: 3.5
          }
        };
      } else {
        //闽南语
        spriteData = {
          tt: { //摇的声音
            start: 0,
            length: 1
          },
          0: { //没中
            start: 2.0,
            length: 2.8
          },
          1: {//一秀
            start: 5.2,
            length: 2.4
          },
          2: { //二举
            start: 8.0,
            length: 2.7
          },
          3: { //四进
            start: 11.0,
            length: 3.5
          },
          4: { //三红
            start: 14.6,
            length: 3.4
          },

          5: { //对堂
            start: 18.0,
            length: 2.8
          },
          6: { //状元
            start: 22.8,
            length: 3.2
          }
        };
      }

      let index = 0;

      let handler = function () {
        if (this.currentTime >= spriteData[index].start + spriteData[index].length) {
          this.pause();
        }
      }
      let audioElem = this.voiceIndex == 1 ? this.$refs.diceAudio1 : this.$refs.diceAudio0;
      audioElem.removeEventListener('timeupdate', handler, false);
      audioElem.addEventListener('timeupdate', handler, false);

      var play = function (rank) {
        audioElem.currentTime = spriteData[rank].start;
        index = rank;
        audioElem.play(rank);
      };

      var pause = function () {
        audioElem.pause();
      }


      return {
        play: play,
        pause: pause
      }
    }
  }
})